package actions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import db.dao.Classes;
import db.dao.ClassesDAO;
import db.dao.Dept;
import db.dao.DeptDAO;
import db.dao.Major;
import db.dao.MajorDAO;
/*添加班级*/
public class AddClassAction extends ActionSupport{
	private String cid="";	//班级编号
	private String cname="";	//班级名称
	private String mid="";	//所属专业编号
	private String did="";	//所属学院编号
	
	public AddClassAction() {
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

	public String getDid() {
		return did;
	}

	public void setDid(String did) {
		this.did = did;
	}
	
	public String execute() throws IOException {
		//获取页面信息用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		//用于获取结果
		String result="";
		//查找班级所属学院
		DeptDAO dDao = new DeptDAO();
		Dept dept = dDao.findById(did);
		//查找班级所属专业
		MajorDAO mDao = new MajorDAO();
		Major major = mDao.findById(mid);
		//设置班级参数
		ClassesDAO cDao = new ClassesDAO();
		Classes classes = new Classes();
		classes.setClassid(cid);
		classes.setClassname(cname);
		classes.setMajor(major);
		classes.setDept(dept);
		classes.setStunumbers((byte)0);
		//保存班级
		result = cDao.save(classes);
		if (result.equals("success")) {
			//如果成功弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('添加成功!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}else {
			//如果失败弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('添加失败!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
